using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;

using NUnit.Framework;


using OWED.Network.Messages;
using OWED.Network.Messages.General;
using OWED.Network.Messages.Reply;

using OWED.Network;

using System.Threading;

namespace OWED.Network
{

        class MessageHandler : Server
        {
            public MessageHandler(int port) : base(port) { }

            protected override void ConnectionLoop(Object me)
            {                
                IProtocolItem l = Wait(me);
                Console.WriteLine("Got:{0}", l);
                if ((l as Login) == null)
                {
                    Console.WriteLine("Login type failure:");
                    Console.WriteLine(l);
                    Reply(me, new PassFail(false,
                        new Reason(1, "YOu have to login first!")));
                }
                else {
                    Reply(me, new PassFail(true, new Reason(0, "Sucessful login!")));
                }

                // THIS IS THE PART THAT EXECUTES THE MESSAGES!!!
                while (true)
                {
                    IProtocolItem msg = Wait(me);
                    String messageName = msg.GetType().ToString().Substring(22, msg.GetType().ToString().Length - 22);\
                    switch (messageName)
                    {
                                case "ApproveChange":
                                    Console.WriteLine("ApproveChange: {0}", messageName);
                                    ApproveChange get = msg as ApproveChange;
                                    Reply(me, new ChangeReply(get.ChangeID));
                                    break;
                        case "Create":
                            Console.WriteLine("Create: {0}", messageName);
                            Create get = msg as Create;
                            break;
                        case "CreateDiscussion":
                            Console.WriteLine("CreateDiscussion: {0}", messageName);
                            CreateDiscussion get = msg as CreateDiscussion;
                            break;
                        case "DeleteDocument":
                            Console.WriteLine("DeleteDocument: {0}", messageName);
                            DeleteDocument get = msg as DeleteDocument;
                            break;
                                case "DenyChange":
                                    Console.WriteLine("DenyChange: {0}", messageName);
                                    DenyChange get = msg as DenyChange;
                                    Reply(me, new ChangeReply(get.ChangeID));
                                    break;
                        case "GetComment":
                            Console.WriteLine("GetComment: {0}", messageName);
                            GetComment get = msg as GetComment;
                            break;
                        case "GetDiscussion":
                            Console.WriteLine("GetDiscussion: {0}", messageName);
                            GetDiscussion get = msg as GetDiscussion;
                            break;
/* hmmm... */           case "GetDocument":
                            Console.WriteLine("GetDocument: {0}", messageName);
                            GetDocument get = msg as GetDocument;
                            break;
/* hmmm... */                   case "GetDocumentList":
                                    Console.WriteLine("GetDocumentList: {0}", messageName);
                                    GetDocumentList get = msg as GetDocumentList;
                                    Reply(me, new DocumentListReply(null)); //TODO:  broken
                                    break;
                        case "GetPermissions":
                            Console.WriteLine("GetPermissions: {0}", messageName);
                            GetPermissions get = msg as GetPermissions;
                            break;
                        case "ImportDocument":
                            Console.WriteLine("ImportDocument: {0}", messageName);
                            ImportDocument get = msg as ImportDocument;
                            break;
/* hmmm... */           case "ListProposals":
                            Console.WriteLine("ListProposals: {0}", messageName);
                            ListProposals get = msg as ListProposals;
                            break;
                        case "Login":
                            Console.WriteLine("Login: {0}", messageName);
                            Login get = msg as Login;
                            break;
                        case "Logout":
                            Console.WriteLine("Logout: {0}", messageName);
                            Logout get = msg as Logout;
                            break;
                        case "PostComment":
                            Console.WriteLine("PostComment: {0}", messageName);
                            PostComment get = msg as PostComment;
                            break;
/* hmmm... */                   case "ProposalList":
                                    Console.WriteLine("ProposalList: {0}", messageName);
                                    ProposalList get = msg as ProposalList;
                                    Reply(me, new OWED.Network.Messages.Reply.ProposalList(get.Proposals));
                                    break;
                        case "SetPermissions":
                            Console.WriteLine("SetPermissions: {0}", messageName);
                            SetPermissions get = msg as SetPermissions;
                            break;
                        case "UpdateDocument":
                            Console.WriteLine("UpdateDocument: {0}", messageName);
                            UpdateDocument get = msg as UpdateDocument;
                            break;
                        default:
                            Console.WriteLine("ERROR! Unhandled message type: {0}", messageName);
                            Reply(me, new PassFail(false, new Reason(0, "Unknown message: {0}.", messageName)));
                            break;
                    }
                    Console.WriteLine("Done!");
                }
            }
            
    }
}